package cn.waka.sys.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.Result;
import cn.waka.sys.admin.object.Star;
import cn.waka.sys.admin.service.StarService;
import cn.waka.sys.admin.utils.Validator;

@RestController
@RequestMapping("/star")
public class StarController extends BaseController {

    @Autowired
    private StarService starService;

    @RequestMapping(path = "/index", name = "明星分页")
    public ModelAndView starList(HttpServletRequest req, HttpServletResponse res) {
        String pStr = req.getParameter("page");
        Page p = new Page();
        if (pStr != null && Validator.isNum(pStr)) {
            p.setPage(Integer.parseInt(pStr));
        }
        String sizeStr = req.getParameter("size");
        if (sizeStr != null && Validator.isNum(sizeStr)) {
            p.setSize(Integer.parseInt(sizeStr));
        }

        String name = req.getParameter("name");
        ModelAndView mv = new ModelAndView();
        Map<String, String> param = new HashMap<String, String>();
        String search_param = "";
        if (name != null) {
            name = name.trim();
        }
        if (name != null && name.length() > 0) {
            param.put("name", "%" + name + "%");
            search_param += "&name=" + name;
            mv.addObject("search_name", name);
        }

        starService.findPage(p, param);

        mv.addObject("page", p);
        mv.addObject("search_param", search_param);
        return mv;

    }

    @RequestMapping(path = "/edit", name = "编辑明星")
    public ModelAndView edit(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        Star star = starService.findById(id);
        if (star == null) {
            star = new Star();
        }
        ModelAndView mv = new ModelAndView();
        mv.addObject("obj", star);
        return mv;
    }

    @RequestMapping(path = "/save", name = "保存明星信息")
    public Result save(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        String name = req.getParameter("name");
        String sexStr = req.getParameter("sex");
        int sex = 0;
        if (sexStr != null && Validator.isNum(sexStr)) {
            sex = Integer.parseInt(sexStr);
        }
        Star star = new Star();
        star.setId(id);
        star.setName(name);
        star.setSex(sex);
        int rs = starService.saveStar(star);

        if (rs > 0) {
            return error(1, "保存成功！");
        } else {
            return error(0, "保存失败！");
        }
    }

    @RequestMapping(path = "/del", name = "删除明星")
    public Result del(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = starService.del(id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/delStarImg", name = "保存明星图片")
    public Result delStarImg(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = starService.delStarImg(id);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }

    @RequestMapping(path = "/addImg", name = "添加明星图片")
    public ModelAndView addImg(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");

        ModelAndView mv = new ModelAndView();
        mv.addObject("id", idStr);
        return mv;
    }

    @RequestMapping(path = "/saveImg", name = "保存明星图片")
    public Result saveImg(HttpServletRequest req, HttpServletResponse res) {
        String idStr = req.getParameter("id");
        String img = req.getParameter("img");
        int id = 0;
        if (idStr != null && Validator.isNum(idStr)) {
            id = Integer.parseInt(idStr);
        }
        int rs = starService.saveImg(id, img);
        if (rs > 0) {
            return error(1, "操作成功！");
        } else {
            return error(0, "操作失败！");
        }
    }
}
